<template>
  <!-- 选择单据 -->
  <div>
    <TopFilter ref="topFilter" :cols="2" :list="topFilterList" :collapse="false" @filterChange="handleSearch"></TopFilter>
    <FilterTable
      :columns="columns"
      columnsRef="partPartPurchasePurchaseOrderCreateBillSelect"
      :dataSource="list"
      :isMemoryPagination="true"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="handleTableRowSelect"
    ></FilterTable>
    <div class="bottomBtnWrap">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleConfirm">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { queryBllsByType, queryPlanOrderList, queryDefaultOrderList, queryBorrowOrderList } from '@/api/part/partPurchase/purchaseOrder';
import { dictionary } from '@/utils/dictMixin';

export default {
  mixins: [dictionary],
  props: {
    billTypeId: Number,
    billTypeList: Array
  },
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      list: [],
      fetchapi: queryBllsByType,
      fetchParams: { xhrAbort: true },
      billType: {}, // 所有单据类型(翻译用)
      selectedRowData: {} // 选中的table行数据
    };
  },
  created() {
    this.billTypeList.map(item => {
      this.billType[item.value] = item.text;
    });
  },
  mounted() {
    this.handleSearch();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'RANGE_DATE',
          label: '单据日期',
          fieldName: 'billDateBefore|billDateAfter'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '单据号',
          dataIndex: this.billTypeId == 97031001 ? 'vBillNo' : 'allocateInNo',
          filter: true,
          filterType: 'input'
        },
        {
          title: '单据类型',
          dataIndex: 'vCorType',
          render: params => {
            return <span>{this.billTypeId == 97031001 ? '采购计划' : this.billTypeId == 97031002 ? '欠料单' : this.billTypeId == 97031003 ? '借入单' : ''}</span>;
          },
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('9703')
        }
      ];
    },
    // 选中table数据
    handleTableRowSelect(e) {
      // console.log(e);
      this.selectedRowData = e;
    },
    // 点击搜索
    async handleSearch(e) {
      if (e && e.billDateBefore) {
        e.billDateBefore = e.billDateBefore ? e.billDateBefore.split(' ')[0] + ' 00:00:00' : '';
        e.billDateAfter = e.billDateAfter ? e.billDateAfter.split(' ')[0] + ' 23:59:59' : '';
      }
      // this.fetchParams = Object.assign(this.fetchParams, e);
      // this.fetchParams.xhrAbort = true;
      // this.fetchParams.xhrAbort = false;
      // console.log(this.billTypeId, 'this.billTypeId');
      if (this.billTypeId == 97031001) {
        let res = await queryPlanOrderList(e);
        if (res.resultCode == 200) {
          this.list = res.data;
          return;
        }
      }
      if (this.billTypeId == 97031002) {
        let res = await queryDefaultOrderList(e);
        if (res.resultCode == 200) {
          this.list = res.data;
          return;
        }
      }
      if (this.billTypeId == 97031003) {
        let res = await queryBorrowOrderList(e);
        if (res.resultCode == 200) {
          this.list = res.data;
          return;
        }
      }
      // switch (this.billTypeId) {
      //   case 97031001:
      //     let res = await queryPlanOrderList(e);
      //     if (res.resultCode == 200) {
      //       this.list = res.data;
      //       consoel.log(this.list, 'list');
      //     }
      //     break;
      //   case 97031002:
      //     let res1 = await queryDefaultOrderList(e);
      //     if (res1.resultCode == 200) {
      //       this.list = res.data;
      //       consoel.log(this.list, 'list');
      //     }
      //     break;
      //   case 97031003:
      //     let res2 = await queryBorrowOrderList(e);
      //     if (res2.resultCode == 200) {
      //       this.list = res.data;
      //       consoel.log(this.list, 'list');
      //     }
      //     break;
      // }
    },
    // 点击确定
    handleConfirm() {
      this.$emit('onConfirm', this.selectedRowData);
      this.handleClose();
    },
    handleClose() {
      this.$emit('close');
    }
  }
};
</script>

<style scoped>
.bottomBtnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
